import json

# 在这里修改要删除的subject值
subject_to_delete = 'None'  # 可以根据需要修改为数字或字母，确保类型与数据库中的一致
path_to_database='assets/database/main_database.json'

try:
    # 读取JSON文件
    with open(path_to_database, 'r') as f:
        data = json.load(f)

    # 过滤掉要删除的subject
    data = [item for item in data if str(item["subject"]) != str(subject_to_delete)]

    # 用于存储已经出现过的subject
    seen = set()
    unique_subjects = []
    for item in data:
        subject = str(item["subject"])
        if subject not in seen:
            unique_subjects.append(subject)
            seen.add(subject)

    print("剩余的所有subject列表:", unique_subjects)

    # 将修改后的数据写回文件
    with open(path_to_database, 'w') as f:
        json.dump(data, f, indent=4)

    print("数据处理完成，已保存到main_database.json")
except FileNotFoundError:
    print("未找到 main_database.json 文件，请检查文件路径。")
except json.JSONDecodeError:
    print("无法解析 main_database.json 文件，请确保文件内容为有效的 JSON 格式。")
except Exception as e:
    print(f"发生未知错误: {e}")